package com.example.dzbsh.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.example.dzbsh.dao.JingshangQingkuangMapper;
import com.example.dzbsh.model.ChaheQingkuang;
import com.example.dzbsh.model.JingshangQingkuang;
import com.example.dzbsh.model.JingshangQingkuangExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class JingshangQingkuangService {
	
	@Resource
	JingshangQingkuangMapper ddMapper;
	@Resource
	ZhenbieQingkuangService ddZhenbieQingkuangService;
	@Resource
	ChaheQingkuangService ddChaheQingkuangService;
	
	public List<Map<String, Object>> findJingshangqingkuangByPage(int ppPageIndex, int ppPageSize){
	
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;

        return ddMapper.findJingshangqingkuangByPage(mmStartIndex, ppPageSize);

    }

    public JingshangQingkuang findOne(String ppId){
		return ddMapper.selectByPrimaryKey(ppId);
	}

	public List<Map<String, Object>> findJiashuByRenyuanIdAndLeixing(String ppRenyuanId,String ppLeixing){
		return ddMapper.findJiashuByRenyuanIdAndLeixing(ppRenyuanId,ppLeixing);
	}

	public List<Map<String,Object>> findByJiashuIdAndLeixing(String ppJiashuId,String ppLeixing){
		return  ddMapper.findByJiashuIdAndLeixing(ppJiashuId,ppLeixing);
	}

	public Integer getCountByzhuangtaiAndRenyuanId(String ppRenyuanId) {
		Object mmCount = ddMapper.getCountByzhuangtaiAndRenyuanId(ppRenyuanId);
		return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
	}

	public Integer countByJiashuId(String ppJiashuId) {
		Object mmCount = ddMapper.countByJiashuId(ppJiashuId);
		return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
	}
	    
	public Integer getCountByisvalid() {
        Object mmCount = ddMapper.getCount();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }
	
	public boolean delete(String ppID){
        return ddMapper.deleteByPrimaryKey(ppID)==1;
    }
	
	public List<JingshangQingkuang> findAll(){
		JingshangQingkuangExample mmExample = new JingshangQingkuangExample();
		mmExample.createCriteria();
        return ddMapper.selectByExample(mmExample);
	}

	public List<Map<String, Object>> findVaild(String ppJiashuId){
		return ddMapper.findValid(ppJiashuId);
	}
	
	public List<JingshangQingkuang> findVaildByRenyuanId(String ppRenyuanId){
		JingshangQingkuangExample mmExample = new JingshangQingkuangExample();
		mmExample.createCriteria().andIsvalidEqualTo(1).andRenyuanidEqualTo(ppRenyuanId);
        return ddMapper.selectByExample(mmExample);
	}

	public List<Map<String,Object>> findVaildByJiashuId(String ppJiashuId){
		List<Map<String,Object>> mmList = ddMapper.findByJiashuId(ppJiashuId);
		List<Map<String,Object>> mmReturnList = new ArrayList<>();
		for (int i = 0; i < mmList.size(); i++) {
			Map<String,Object> mmMap = mmList.get(i);

			int mmShifoubangding = 0;
			if (mmMap!=null){
				String mmJingahangId = mmMap.get("id")==null?"":mmMap.get("id").toString();

				List<ChaheQingkuang> mmBangdingList = ddChaheQingkuangService.findByJingshangId(mmJingahangId);
				if (mmBangdingList.size()!=0){
					mmShifoubangding = 1;

				}

				mmMap.put("shifoubangding",mmShifoubangding);

				mmReturnList.add(mmMap);
			}
		}
		return mmReturnList;
	}

    public int DeleteByRenyuanId(String ppRenyuanId){
		JingshangQingkuangExample mmExanple = new JingshangQingkuangExample();
		mmExanple.createCriteria().andRenyuanidEqualTo(ppRenyuanId);
		return ddMapper.deleteByExample(mmExanple);
	}

	public List<Map<String,Object>> findbyshenfenzhenghaoandShehuixinyongdaima(String ppShenfenzhenghao,String ppShehuixingyongdaima){
		return ddMapper.findbyshenfenzhenghaoandShehuixinyongdaima(ppShenfenzhenghao,ppShehuixingyongdaima);
	}
}
